package com.shijianchen.utils;

import lombok.Data;

@Data
public class Result<T> {
    private Integer status;
    private String msg;
    private T data;
    public Result(Integer status,T data){
        this.status = status;
        this.data = data;
    }
    public Result(Integer status,String msg){
        this.status = status;
        this.msg = msg;
    }

    /**
     * 返回成功
     * @param <T> data
     * @return 公共返回类
     */
    public static <T> Result<T> ok(T data){
        return new Result<>(ResultCode.HTTP_CODE_SUCCESS.getStatus(),data);
    }

    /**
     * 返回成功
     * @param msg 信息
     * @return 公共返回类
     */
    public static <T> Result<T> ok(String msg){
        return new Result<>(ResultCode.HTTP_CODE_SUCCESS.getStatus(),msg);
    }

    /**
     * 返回失败
     * @param <T> data
     * @return 公共返回类
     */
    public static <T> Result<T> fail(T data){
        return new Result<>(ResultCode.HTTP_CODE_FAIL.getStatus(),data);
    }

    /**
     * 返回失败
     * @param msg 信息
     * @return 公共返回类
     */
    public static <T> Result<T> fail(String msg){
        return new Result<>(ResultCode.HTTP_CODE_FAIL.getStatus(),msg);
    }
    public static <T> Result<T> fail(Integer status,String msg){
        return new Result<>(status,msg);
    }

    /**
     * 返回失败
     * @param res 信息
     * @return 公共返回类
     */
    public static <T> Result<T> fail(ResultCode res){
        return new Result<>(res.getStatus(),res.getName());
    }
}
